/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include <memory>

#include "DBManager/Database/IDatabase.h"

SG_DBMANAGER_NAMESPACE_OPEN

class DBServiceFactory;

/// @brief DBM 智能指针
using DBServiceFactorySP = std::shared_ptr<DBServiceFactory>;

/**
 * @brief    管理Service的工厂
 *  通过get函数获取所想要操作数据的Service
 * @author   wuxingchuang
 * @date     2025.02.20
 */
class DBManager_DECLSPEC DBServiceFactory
{
  public:
    explicit DBServiceFactory (IDatabase* database);
    ~DBServiceFactory ();

    /// @brief 清空当前数据库
    void ClearDatabase ();

    /// @brief 保存当前数据库数据（写出缓存）
    void SaveDatabase ();

    /// @brief 连接数据库
    void ConnectDatabase ();

    /// @brief 断开数据库连接
    void DisconnectDatabase ();

    /// @brief  获取对象实例
    template <class T>
    std::shared_ptr<T> get ();

    IDatabase* GetDatabase () const
    {
        return m_Database;
    }

  private:
    IDatabase* m_Database;  // 当前数据库

    std::map<std::size_t, std::shared_ptr<void>> m_ServiceRep;
};

SG_DBMANAGER_NAMESPACE_CLOSE
